package guijavacommander;

import javax.swing.*;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import java.io.File;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * User: Deady
 * Date: 15.07.2009
 * Time: 11:30:09
 */
public class TreeListModel extends DefaultListModel {

    private TreeModel treeModel;
    private TreeNode currentParent;
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public TreeListModel(TreeModel treeModel) {
        this.treeModel = treeModel;
        currentParent = (TreeNode) treeModel.getRoot();
    }

    @Override
    public Object getElementAt(int index) {
        int childIndex;
        if (currentParent.equals(treeModel.getRoot())) {
            childIndex = index;
        }
        else {
            if (index==0) {
                return new FileTreeNode(new File(".."));
            }
            childIndex = index-1;
        }
        return treeModel.getChild(currentParent, childIndex);
    }

    @Override
    public int getSize() {
        if (currentParent.equals(treeModel.getRoot())) {
            return treeModel.getChildCount(currentParent);
        }
        return treeModel.getChildCount(currentParent)+1;
    }

    public int gotoParent() {
        int index = 0;
        if (!currentParent.equals(treeModel.getRoot())) {
            TreeNode newParent = currentParent.getParent();
            index = newParent.getIndex(currentParent)+(!newParent.equals(treeModel.getRoot())?1:0);
            setCurrentParent(newParent);
        }
        return index;
    }


    public int gotoChild(int index) {
        if (currentParent.equals(treeModel.getRoot())) {
            TreeNode child = (TreeNode) getElementAt(index);
            setCurrentParent(child);
        }
        else {
            if (index==0) {
                return gotoParent();
            }
            else {
                TreeNode child = (TreeNode) getElementAt(index);
                setCurrentParent(child);
            }
        }
        return 0;
    }

    public TreeNode getCurrentParent() {
        return currentParent;
    }

    public void setCurrentParent(TreeNode parent) {
        TreeNode old = currentParent;
        currentParent = parent;
        propertyChangeSupport.firePropertyChange("currentParent", old, currentParent);
        refresh();
    }


    public void refresh() {
        fireContentsChanged(this, 0, getSize()-1);
    }


    public TreeModel getTreeModel() {
        return treeModel;
    }

    public boolean isCellEditable(int index) {
        return true;
    }
    
    public void setValueAt(Object value, int index) {

    }


    public void addPropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
    }

    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
    }
}
